package com.example.userlogin.controller;


import com.example.userlogin.entity.Employee;
import com.example.userlogin.entity.Goods;
import com.example.userlogin.entity.Supplier;
import com.example.userlogin.service.EmployeeService;
import com.example.userlogin.service.GoodsService;
import com.example.userlogin.service.SupplierService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/employee")
//专门处理供应商控制器
public class EmployeeController {
    //    实现与前端交互的数据请求方法
//    1.进入模块首页的时候需要对数据里的所有商品数据进行查询，返回列表给前端页面进行展示
    @Autowired
    EmployeeService employeeService;

    @Autowired
    HttpServletRequest request;

    //    1.展示数据库里已有的商品列表给前端
    @RequestMapping("/employeeList")
    public String EmployeeMain(Model model){
//        调用service方法查询到商品列表结果
        List<Employee> employeeList = employeeService.findAll();
        Object name = request.getSession().getAttribute("name");
//        将查询结果用model绑定
        model.addAttribute("data",employeeList);
        model.addAttribute("username",name);
        System.out.println(employeeList);
//        返回视图
        return "employee/employeeList";
    }

    @RequestMapping("/employeeAdd")
    public String employeeAdd(Model model){
        Object name = request.getSession().getAttribute("name");
        model.addAttribute("name",name);
        return "employee/employeeAdd";
    }

    @RequestMapping("/employeeAddProcess")
    public String employeeAddProcess(@RequestParam String name, @RequestParam String telephone,
                                     @RequestParam String hiretime, @RequestParam String jobtitile,
                                     @RequestParam String department,@RequestParam double salary
    ){
        //      组装一个goods实体对象
        Employee  employee = new Employee();
        employee.setName(name);
        employee.setTelephone(telephone);
        employee.setHiretime(hiretime);
        employee.setJobtitile(jobtitile);
        employee.setDepartment(department);
        employee.setSalary(Double.valueOf(salary));
        //        调用service的add方法给数据库
        if (employeeService.add(employee)==1){
            //        如果返回ok，那么就进入商品管理首页
            return "redirect:/employee/employeeList";
        }else {
            return "redirect:/employee/employeeList";
        }
    }

    @RequestMapping("/employeeUpdate")
    public String update(@RequestParam String id,Model model){
//        首先基于id查询出来该id记录里的所有数据信息
        Employee employee=new Employee();
        employee.setId(Integer.valueOf(id));
//        调用service方法查询出来该记录的数据信息
        Employee employeeDB = employeeService.findOne(employee);
//        2.将这些数据信息返回给视图
        model.addAttribute("data",employeeDB);
        Object name = request.getSession().getAttribute("name");
        model.addAttribute("name",name);
//        3.返回视图文件
        return "employee/employeeUpdate";
    }

    @RequestMapping("/employeeUpdateProcess")
    public String employeeUpdateProcess(@RequestParam String name,@RequestParam String telephone,
                                        @RequestParam String hiretime, @RequestParam String jobtitile,
                                        @RequestParam String department,@RequestParam double salary){
        //      组装一个goods实体对象
        Employee  employee = new Employee();
        employee.setName(name);
        employee.setTelephone(telephone);
        employee.setHiretime(hiretime);
        employee.setJobtitile(jobtitile);
        employee.setDepartment(department);
        employee.setSalary(Double.valueOf(salary));
        //        调用service的add方法给数据库
        if (employeeService.add(employee)==1){
            //        如果返回ok，那么就进入商品管理首页
            return "redirect:/employee";
        }else {
            return "redirect:/employee";
        }
    }

}
